# -*- coding:UTF-8 -*-

from django.db import models
from django.conf import settings


# 订单模型类
class Work(models.Model):
    # 商家名称
    name = models.CharField(max_length=20, verbose_name='商家名称')
    # 订单类型 1为批量 2为自主
    type = models.IntegerField(verbose_name='订单类型')
    # 订单状态 默认为1：待发布,2：进行中 3：完成
    state = models.IntegerField(verbose_name='订单完成状态', default=1)
    # 备注
    remark = models.TextField(verbose_name='备注', null=True, default=None)
    # 模板
    template = models.CharField(max_length=20, verbose_name='模板', null=True, default=None)
    # 录入人
    create_user = models.CharField(max_length=10, verbose_name='录入人', null=True, default=None)
    # 录入时间
    create_date = models.DateField(verbose_name='录入时间', default=None)

    class Meta:
        db_table = 'tb_work'
        verbose_name = '订单'
        verbose_name_plural = verbose_name


# 书籍模型类
class Books(models.Model):
    name = models.CharField(max_length=20, verbose_name="图书名字", default='无')
    code = models.CharField(max_length=20, verbose_name='书籍编号', unique=True, null=False)
    work = models.ForeignKey(Work, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='bookOfWork')
    create_date = models.DateField(auto_now_add=True, verbose_name='创建时间')
    finished = models.BooleanField(verbose_name='完成状态', default=False)
    return_back = models.BooleanField(verbose_name="是否归还物品", default=False)
    class Meta:
        db_table = 'tb_books'
        verbose_name = '书籍'
        verbose_name_plural = verbose_name


# 任务阶段模型类
class StageName(models.Model):
    name = models.CharField(max_length=10, verbose_name='阶段名称', default=None)

    class Meta:
        db_table = 'tb_stage_name'
        verbose_name = '任务阶段名称'
        verbose_name_plural = verbose_name


# 任务模型类
class Task(models.Model):
    # 录入图书的编号
    code = models.ForeignKey(Books, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='taskOfBook')
    # 当前阶段的名称 1为录入 2为前期 3为分片 4为后期 5最终审核 6完成
    stage = models.ForeignKey(StageName, on_delete=models.CASCADE, db_constraint=False, null=True, default=1, related_name='taskOfStage')
    # 当前任务阶段类型 1为平面 2为视频
    stage_type = models.IntegerField(verbose_name='当前阶段类型', default=None)
    # 当前阶段的状态 1为等待分配 2为进行中 3为已完成 4待审核
    state = models.IntegerField(verbose_name='当前任务阶段状态')
    # 下一阶段名称
    next_stage = models.IntegerField(verbose_name='下一阶段名称')
    # 创建任务的时间
    create_date = models.DateField(auto_now_add=True, verbose_name='创建时间')
    # 任务创建人
    create_user = models.CharField(max_length=10, verbose_name='创建人', default="Null")
    # 任务到期时间
    diedline = models.DateField(verbose_name='任务截止时间')

    class Meta:
        db_table = 'tb_task'
        verbose_name = '任务'
        verbose_name_plural = verbose_name


# 历史记录
class History(models.Model):
    book = models.ForeignKey(Books, on_delete=models.CASCADE, db_constraint=False,
                             null=True, default=1, related_name='historyOfBook')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, default=1, related_name='historyOfTask')
    stage = models.ForeignKey(StageName, on_delete=models.CASCADE, db_constraint=False,
                              null=True, default=1, related_name='historyOfStage')
    start_time = models.DateField(verbose_name='开始时间')
    finish_time = models.DateField(verbose_name='完成时间', null=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='historyOfUsers')

    class Meta:
        db_table = 'tb_history'
        verbose_name = '历史记录'
        verbose_name_plural = verbose_name


# 任务人员
# class TaskUser(models.Model):
#     task = models.ManyToManyField(Task, related_name='UserOfTask')
#     user = models.OneToOneField(Users, on_delete=models.CASCADE, null=True)
#     type = models.IntegerField(verbose_name='任务人员负责的部分')
#
#     class Meta:
#         db_table = 'tb_task_user'
#         verbose_name = '任务人员'
#         verbose_name_plural = verbose_name
